<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/17 0017
  Time: 上午 10:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>投票管理-添加</title>

    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">

    <link href="/static/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <link href="/static/plugins/bootstrap-table/bootstrap-table.css" rel="stylesheet" >



<%--<link rel="stylesheet" href="/static/css/common.css">--%>

</head>
<body>
<div class="wrap-padding-15">
    <span class="layui-breadcrumb">
          <a href="/" target="_top">首页</a>
          <a href="/vote">投票管理</a>
          <a><cite>编辑</cite></a>
     </span>

     <div class="layui-tab layui-tab-card" lay-filter="vote_tab">
         <ul class="layui-tab-title">
             <li lay-id="1" class="layui-this">创建投票</li>
             <li lay-id="2">投票项</li>
         </ul>
        <div class="layui-tab-content" >
            <div class="layui-tab-item layui-show">
                <input type="hidden" name="tabId" id="tabId" value="${param.tabId}">
                <input type="hidden" name="voteId" id="voteId" value="${vote.id}">
                <form  method="post" class="layui-form">
                    <input type="hidden" name="id" id="id" value="${vote.id}">

                    <div class="layui-form-item">
                        <label class="layui-form-label">名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="title"  value="${vote.title}" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">Banner：</label>
                        <div class="layui-input-block">
                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="test1">上传图片</button>
                                <input type="hidden" name="banner" id="banner" value="${vote.banner}">
                                <div class="layui-upload-list">
                                    <img style="width: 100px; height: 100px" class="layui-upload-img" id="demo1" src="/dl/img?path=${vote.banner}">
                                    <p id="demoText"></p>
                                </div>
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">起止时间：</label>
                        <div class="layui-input-block">
                            <div class="site-demo-laydate">
                                <input type="text" name="qzTime" value="<fmt:formatDate  value="${vote.startTime}"  pattern="yyyy-MM-dd HH:mm:ss"/> ~ <fmt:formatDate  value="${vote.endTime}" pattern="yyyy-MM-dd HH:mm:ss"/>"  id="time"  placeholder="-"  class="layui-input">

                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态：</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="status" value="1" title="发布" <c:if test="${vote.status eq 1}">checked</c:if>>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formSubmit">提交</button>
                            <a href="/vote"><button type="button" class="layui-btn layui-btn-primary">取消</button></a>
                        </div>
                    </div>

                </form>
            </div>
            <div class="layui-tab-item">
                <div id="toolbar" class="layui-btn-group">
                    <button id="addBtn" data-url="/voteitem/add" class="layui-btn layui-btn-primary btn-add">增加</button>
                    <button id="editBtn" data-url="/voteitem/edit" class="layui-btn layui-btn-primary btn-edit" >编辑</button>
                    <button id="delBtn" data-url="/voteitem/delete" class="layui-btn layui-btn-primary btn-del">删除</button>
                </div>
                <table id="tb_voteitem"></table>
                <div style="text-align: center; margin-top: 5px">
                    <a href="/vote"><button type="button" class="layui-btn">完成</button></a>
                </div>

            </div>
        </div>
     </div>



</div>





<script src="/static/plugins/jquery.js"></script>
<script src="/static/plugins/layui/layui.js"></script>
<script src="/static/plugins/bootstrap/js/bootstrap.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table-zh-CN.js"></script>


<script>

    layui.use(['element','layer','form','upload','laydate'], function(){
        var form = layui.form,
         upload = layui.upload,
        laydate = layui.laydate,
         element = layui.element;


        var tabId = $("#tabId").val()
        if(tabId==2){
            element.tabChange("vote_tab", "2")
        }
        //监听提交
        form.on('submit(formSubmit)',function (data) {
            $.ajax({
                url:"/vote/update",
                type:"post",
                dataType:"json",
                data:data.field,
                success:function (rs) {
                    layer.msg(rs.message,{icon:1})
                    /*if(rs.code==1){
                        element.tabChange("vote_tab","2")
                    }*/
                    location.href = "/vote"
                },
                error:function () {
                    layer.msg("服务器异常，请稍后再试")
                }
            })
            return false
        })

        $('#tb_voteitem').bootstrapTable({
            url: '/voteitem/listData',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式

            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            search: true,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: true,
            showColumns: true,                  //是否显示所有的列
            showRefresh: true,                  //是否显示刷新按钮
            //minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            height: 800,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            //showToggle:true,                    //是否显示详细视图和列表视图的切换按钮
            //cardView: false,                    //是否显示详细视图
            detailView: false,                  //是否显示父子表
            queryParams: function queryParamFun(data) {
                var param = {
                    offset:data.offset,
                    limit:data.limit,
                    voteId:$("#voteId").val()
                }
                return param;
            },//传递参数（*）

            columns: [{
                checkbox: true
            },{
                field: 'sortNo',
                title: '编号',
                align:'center',
                // width:'33%',

            }, {
                field: 'name',
                title: '名称',
                align:'center',
                // width:'33%',

            }, {
                field: 'img',
                title: '图片',
                align:'center',
                //width: '30%',
                formatter: function (value, row, index) {
                    var img = '<img style="width: 53px; height: 53px" src="/dl/img?path=' + value + '"/>';
                    return img;
                }
            },{
                field: 'votes',
                title: '票数',
                align:'center',
                // width:'33%',

            }, ],

        });

        $("#addBtn").on('click',btnAdd)
        $("#editBtn").on('click',btnEdit);
        $("#delBtn").on('click',btnDel);


        function btnAdd(){
            var voteId = $("#voteId").val()
            if(null == voteId){
                layer.msg("请先添加投票")
                return false
            }
            var url = $(this).data("url")
            layer.open({
                type:2,
                title:"添加投票项",
                area:['500px','500px'],
                content:url+"?voteId="+voteId
            })
        }

        function btnEdit(){
            var rows = $("#tb_voteitem").bootstrapTable('getSelections');
            if(rows.length < 1) {
                layer.msg("请勾选一条数据");
            }else if(rows.length>1){
                layer.msg("只能勾选一条数据");
            }else{
                var id = (rows)[0].id
                var url = $(this).data("url")
                layer.open({
                    type:2,
                    title:"编辑投票项",
                    area:['500px','600px'],
                    content:url+"?id="+id
                })
            }
        }

        function btnDel(){

            var rows = $("#tb_voteitem").bootstrapTable('getSelections');

            var rowIds = [];
            $(rows).each(function(){
                var id = this.id;
                rowIds.push(id);
            });

            //异步请求后台删除，所选行记录
            if(rows.length < 1) {
                layer.msg("请勾选至少一条数据");
            }else {
                layer.confirm("确定删除所选数据吗？",function (index) {
                    //执行删除操作
                    $.ajax({
                        url: "/voteitem/delete",
                        type: "post",
                        dataType: 'json',
                        data: {ids : rowIds.join(",")},
                        success:function(msg){
                            layer.msg(msg.message);
                            if(msg.code == 1) {
                                $("#tb_voteitem").bootstrapTable("refresh");  //刷新表格数据
                            }
                        },
                        error:function(e){
                            layer.msg("服务器忙，请稍后再试");
                        }

                    });
                });
            }
        }


    //日期时间范围
        laydate.render({
            elem: '#time'
            ,type: 'datetime'
            ,range:'~'
        });

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/upload'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
              /*  if(res.code > 0){
                    return layer.msg('上传失败');
                }*/
                $("#banner").val(res[0].src);

                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


    });


</script>
</body>
</html>
